package com.yunhe.servlet;

import java.io.IOException;

import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.OrderDetailedInfo;
import com.yunhe.bean.Page_2;
import com.yunhe.inter.OrderDetailedInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * 用来展示订单信息
 */
@WebServlet("/MyOrderInfoServlet")
public class MyOrderInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public MyOrderInfoServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//建立数据库连接
				SqlSession sqlSession =SqlSessionFactoryUtil.openSqlSession();
				OrderDetailedInfoInter mapper = sqlSession.getMapper(OrderDetailedInfoInter.class);
				//创建分页信息对象
				Page_2 pag = new Page_2();
				/**
				 * 处理当前页的值
				 * 设置当前页的默认值为1（首次登陆页面默认值为1）
				 * 如果不是第一次登陆则当前页获取页面传递的currentPage值
				 * 将当前页的值传入封装的对象中
				 * 
				 */
				int currentPage=1;
				if(request.getParameter("currentPage")!=null){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
				 pag.setCurrentPage(currentPage);
				 
				 /**
				  * 处理总页数
				  * 查询出记录总条数
				  * 总页数等于总记录数除以每页所要显示的记录数，if用来处理不被整除的情况
				  * 
				  */
				 int totalPage = mapper.findTotalNumber();
				 if(totalPage%pag.getPageNumber()==0){
					 pag.setSumPage(totalPage/pag.getPageNumber());
				 }else{
					 pag.setSumPage(totalPage/pag.getPageNumber()+1);
				 }
				 
				 /**
				  * 处理rownum中的两个参数，开始数和结束数
				  */
				 pag.setEndPage(pag.getCurrentPage()*pag.getPageNumber());
				 pag.setBeginPage(pag.getEndPage()-pag.getPageNumber());
				 
				//把处理过后的对象存放到request中供页面调用
				request.setAttribute("pag", pag);
				//获取所对应记录（三个参数分别为用户id，开始分页的开始和结束）
				
				ArrayList<OrderDetailedInfo> odiList = mapper.showMyOrderInfo(1, pag.getBeginPage(),pag.getEndPage());
				request.setAttribute("odiList", odiList);
				sqlSession.close();
				request.getRequestDispatcher("/myOrderInfo.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
